package com.openstack4j.qx.imp;

import org.openstack4j.api.Builders;
import org.openstack4j.api.OSClient.OSClientV2;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.compute.Flavor;
import org.openstack4j.openstack.OSFactory;

import com.openstack4j.qx.ResourceTemplateController;
import com.openstack4j.qx.pojo.BaseResp;


public class ResourceTemplateControllerImp implements ResourceTemplateController{

	
	/**
	 * 创建资源模板
	 * @baater
	 * 内蒙古云科数据服务有限公司
	 */
	public BaseResp createResourceTemplate(String resourceTemplateID, String resourceTemplateXml) {
		//login and authentication 登录认证
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.11:5000/v2.0")
		                       .credentials("admin","ADMIN_PASS")
		                       .tenantName("admin")
		                       .authenticate();
	
		//
		Flavor flavor = Builders.flavor()
                .name(resourceTemplateID)   //临时resourceTemplateID 该为 flavor name
                .ram(1024)
                .vcpus(1)
                .disk(2)
                .swap(2)
                //.rxtxFactor(1.11f)
                .build();

		 Flavor f = os.compute().flavors().create(flavor);
		
		 
		 
		
		//实例化  创资源模板返回 对象
		BaseResp baseResp = new BaseResp();
		
		
		if(!f.equals("success")){
			baseResp.setFaultstring("创建资源模板失败！");
		 }
		 
		
		return baseResp;
	}

	
	
	
	/**
	 * 删除资源模板
	 * @baater
	 * 内蒙古云科数据服务有限公司
	 */
	public BaseResp DeleteResourceTemplate(String resourceTemplateID) {
		//login and authentication֤
		OSClientV2 os = OSFactory.builderV2()
		                       .endpoint("http://10.50.0.11:5000/v2.0")
		                       .credentials("admin","ADMIN_PASS")
		                       .tenantName("admin")
		                       .authenticate();
		
		//delete flavor by id
		ActionResponse ar = os.compute().flavors().delete(resourceTemplateID);
		//initialize the delete class
		BaseResp baseResp = new BaseResp();
		
        //deleteResourceTemplateResp.setFaultstring(ar.getFault());
		// 返回内容为：Flavor 28059626-a1d0-439b-af8c-d0acb39dec33 could not be found.
		
		
		if(!ar.isSuccess()){
			baseResp.setFaultstring("没有找到资源模板"+resourceTemplateID);
		}else{
			baseResp.setFaultstring("成功删除资源模板"+resourceTemplateID);
		}
		
				
		return baseResp;
	}
	
}
